package io.kiki.sba.registry.server.clientInterface.converter;

import io.kiki.sba.registry.common.model.store.BaseInfo.ClientVersion;
import io.kiki.sba.registry.common.model.store.DataInfo;
import io.kiki.sba.registry.common.model.store.Subscriber;
import io.kiki.sba.registry.common.model.store.Url;
import io.kiki.sba.registry.common.model.store.Watcher;
import io.kiki.sba.registry.converter.ScopeEnumConverter;
import io.kiki.sba.registry.core.model.ConfiguratorRegister;
import io.kiki.sba.registry.core.model.SubscriberRegister;


public final class SubscriberConverter {
    private SubscriberConverter() {
    }

    public static Subscriber convert(SubscriberRegister subscriberRegister) {

        Converter<SubscriberRegister, Subscriber> converter = source -> {
            Subscriber subscriber = new Subscriber();

            subscriber.setAppName(source.getAppName());
            subscriber.setZone(source.getZone());
            subscriber.setClientId(source.getClientId());
            subscriber.setDataId(source.getDataId());
            subscriber.setGroup(source.getGroup());
            subscriber.setInstanceId(source.getInstanceId());
            subscriber.setRegisterId(source.getRegistId());
            subscriber.setProcessId(source.getProcessId());
            subscriber.setVersion(source.getVersion());
            subscriber.setRegisterTimestamp(System.currentTimeMillis());
            subscriber.setClientRegisterTimestamp(source.getTimestamp());
            subscriber.setScope(ScopeEnumConverter.convertToScope(source.getScope()));
            subscriber.setClientUrl(new Url(source.getIp(), source.getPort()));
            subscriber.setAttributes(source.getAttributes());
            subscriber.setClientVersion(ClientVersion.StoreData);
            subscriber.internAcceptEncoding(source.getAcceptEncoding());
            subscriber.setAcceptMulti(source.isAcceptMulti());

            DataInfo dataInfo = new DataInfo(source.getInstanceId(), source.getDataId(), source.getGroup());

            subscriber.setDataInfoId(dataInfo.getDataInfoId());

            return subscriber;
        };
        return converter.convert(subscriberRegister);
    }


    public static Watcher convert(ConfiguratorRegister configuratorRegister) {
        Converter<ConfiguratorRegister, Watcher> converter = source -> {
            Watcher watcher = new Watcher();

            watcher.setAppName(source.getAppName());
            watcher.setZone(source.getZone());
            watcher.setClientId(source.getClientId());
            watcher.setDataId(source.getDataId());
            watcher.setGroup(source.getGroup());
            watcher.setInstanceId(source.getInstanceId());
            watcher.setRegisterId(source.getRegistId());
            watcher.setProcessId(source.getProcessId());
            watcher.setVersion(source.getVersion());
            watcher.setAttributes(source.getAttributes());
            if (source.getTimestamp() != null) {
                watcher.setClientRegisterTimestamp(source.getTimestamp());
            }
            watcher.setRegisterTimestamp(source.getTimestamp());
            watcher.setClientUrl(new Url(source.getIp(), source.getPort()));

            watcher.setClientVersion(ClientVersion.StoreData);

            DataInfo dataInfo = new DataInfo(source.getInstanceId(), source.getDataId(), source.getGroup());

            watcher.setDataInfoId(dataInfo.getDataInfoId());

            return watcher;
        };
        return converter.convert(configuratorRegister);
    }
}
